package com.zero.dubbo.sample;

import java.util.*;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) {
        System.out.println( "Hello World!" );

        List<Map<String, String>> lists = new ArrayList<>();

        Map<String, String> map = new HashMap<>();
        map.put("a","12");
        map.put("s","18");
        map.put("c","13");
        map.put("r","11");
        map.put("i","16");
        map.put("u","17");

        Map<String, String> map2 = new HashMap<>();
        map2.put("a","12");
        map2.put("s","18");
        map2.put("c","13");
        map2.put("r","11");
        map2.put("i","16");
        map2.put("u","17");


        Map<String, String> map3 = new HashMap<>();
        map3.put("a","12");
        map3.put("s","18");
        map3.put("c","13");
        map3.put("r","11");
        map3.put("i","16");
        map3.put("u","17");

        lists.add(map);
        lists.add(map2);
        lists.add(map3);

        final Map<String, Integer> results = new HashMap<String, Integer>();
        getSumValueMap(lists,results,"a");
        getSumValueMap(lists,results,"s");
        getSumValueMap(lists,results,"c");
        getSumValueMap(lists,results,"r");
        getSumValueMap(lists,results,"i");
        getSumValueMap(lists,results,"u");

        System.out.println(results);



        Map<String, Integer> sortMapByValue = sortMapByValue(results);

        for(Map.Entry<String, Integer> entry: sortMapByValue.entrySet()){
            System.out.println(entry.getKey()+","+entry.getValue());
            break;
        }

        System.out.println(sortMapByValue);


    }

    private static void getSumValueMap(List<Map<String, String>> eventList,Map<String, Integer> results,String key){
        for (Map<String, String> map : eventList) {
            if(results.get(key) == null){
                results.put(key,  Integer.parseInt(map.get(key)));
            }else{
                results.put(key,  results.get(key) + Integer.parseInt(map.get(key)));
            }
        }
    }

    public static Map<String,Integer> sortMapByValue(Map<String,Integer> oriMap){
        if(oriMap == null || oriMap.isEmpty()){
            return null;
        }

        Map<String, Integer> sortMap = new LinkedHashMap<>();
        List<Map.Entry<String, Integer>> entryList = new ArrayList<>(oriMap.entrySet());

        Collections.sort(entryList,new MapValueComparator());
        Map.Entry<String, Integer> tmpEntry = null;
        Iterator<Map.Entry<String, Integer>> iter = entryList.iterator();
        while (iter.hasNext()){
            tmpEntry = iter.next();
            sortMap.put(tmpEntry.getKey(),tmpEntry.getValue());
        }

        return sortMap;
    }

    public static class MapValueComparator implements Comparator<Map.Entry<String,Integer>>{
        @Override
        public int compare(Map.Entry<String, Integer> me1, Map.Entry<String, Integer> me2) {
            return me2.getValue().compareTo(me1.getValue());
        }
    }
}
